/**
 * Created by MD-3 on 2017/6/13.
 */

function updateMap() {
    $.ajax({
        type	: "POST",
        url		: "/sys/door/event?nocache="+new Date().getTime(),
        timeout : 30000,
        dataType: "json",
        success : function(str){
            if(!str || 0!=str.code){
                return;
            }
            updateDoorData(str.data);

        },
        error   : function(XMLHttpRequest, textStatus, errorThrown){
            console.log("taskAjax error occured");
            console.log("status:"+XMLHttpRequest.status);
            console.log("readyState:"+XMLHttpRequest.readyState);
            console.log("textStatus:"+textStatus);
            console.log("errorThrown:"+errorThrown);
            console.log("");
            /*
             XMLHttpRequest.status: 状态码的意思
             200 - 确定。客户端请求已成功。
             202 - 请求已被接收，但尚未成功
             400 - 错误请求，如语法错误
             401 - 访问被拒绝
             403 - 禁止访问(由于服务器上文件或目录的权限设置导致资源不可用)
             404 - 没有发现文件、查询或URl(没有找到指定的资源)
             500 - 服务器产生内部错误
             XMLHttpRequest.readyState: 状态码的意思
             0 － （未初始化）还没有调用send()方法
             1 － （载入）已调用send()方法，正在发送请求
             2 － （载入完成）send()方法执行完成，已经接收到全部响应内容
             3 － （交互）正在解析响应内容
             4 － （完成）响应内容解析完成，可以在客户端调用了
             textStatus是Ajax内部自己处理的，其值可以是"timeout", "error", "null","parsererror"等。
             */
        }
    });
}

function  updateDoorData(data) {
    var idList = data;
    var doorId, action;
    for(var i in idList){
        doorId = idList[i].doorId;
        action = idList[i].action;
        if("0" == action){
            closeDoor(doorId);
        }else if("1" == action) {
            openDoor(doorId);
        }
    }
}
/*
function updateDoor(doorId, action) {
    var doorConstruct = getDoorById(doorId);

    var doorState = doorConstruct.doorState = "1";
    var doorMarker = doorConstruct.doorMarker;

    var inon = getDoorIcon(doorState);
    console.log(doorMarker.getIcon()+"----"+inon);
    doorMarker.setIcon(new AMap.Icon({image:inon}));
    openInfoWindow(doorId);

    setTimeout('closeDoor('+doorId+')', 4*1000);
}*/

function openDoor(doorId) {
    var doorConstruct = getDoorById(doorId);
    var doorState = doorConstruct.doorState;
    var alarmState = doorConstruct.alarmState = "1";
    if(doorState == "1"){
        return;
    }
    var doorMarker = doorConstruct.doorMarker;
    doorState = doorConstruct.doorState = "1";
    doorMarker.setIcon(getDoorIcon(doorState,alarmState));
    openInfoWindow(doorId);
}

function closeDoor(doorId) {
    var doorConstruct = getDoorById(doorId);
    var doorState = doorConstruct.doorState;
    var alarmState = doorConstruct.alarmState;
    if(doorState == "0"){
        return;
    }
    var doorMarker = doorConstruct.doorMarker;
    doorState = doorConstruct.doorState = "0";
    doorMarker.setIcon(getDoorIcon(doorState,alarmState));
    closeInfoWindow();
}

function openDoorAjax(doorId) {
    $.ajax({
        type	: "POST",
        url		: "/sys/door/open?nocache="+new Date().getTime(),
        data	: {doorId:doorId},
        timeout : 30000,
        dataType: "json",
        success : function(){
            console.log("openDoorAjax is occured");
        },
        error   : function(e){
            console.log("openDoorAjax error occured");
        }
    });
}
